var is_run = true;
var d_x = 0;
var d_y = 0;
var all_reward = 0;
var key_event = 0;
var action=0;

alert("上下左右：移动;Z，X：攻击")

// 初始化
draw(topy("reset",[4]));

// 开始更新
window.refreshEvent = setInterval(refresh, 50, false);
function refresh() {
    if (is_run) {

        var step_result = topy('step',[action]);
        action = 0;

        all_reward += step_result[1];
        $("span").text(String(all_reward));

        draw(step_result[0]);

        // console.log(step_result[0].length,"",step_result[1],"",step_result[2]);
        done=step_result[2]
        if(done==1)
        {
            alert("你输了！");
        }else if(done==-1){
            alert("你赢了！");
        }
    }
}

$("button").click(function () {
    is_run = !is_run;
})

$(document).keydown(function (event) {
    // console.log(event);
    key_event = event.which;
    if (key_event == 38) {
        action = 1;
    }else if(key_event==40){
        action=2;
    }else if(key_event==37){
        action=3;
    }else if(key_event==39){
        action=4;
    }else if(key_event==90){
        action=5;
    }else if(key_event==88){
        action=6;
    }
});